package com.xiaoyu.merchant.ui.activity.order;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.model.AfterSaleBean;
import com.xiaoyu.merchant.network.NetworkConstants;
import com.xiaoyu.merchant.network.NetworkManager;
import com.xiaoyu.merchant.ui.dialog.HandleRejectionDialog;
import com.xiaoyu.merchant.ui.fragment.order.OrderFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author jx
 * @date 创建时间 2019/1/4
 * @Description 售后处理
 */
public class AfterProcessingActivity extends BaseActivityWithTitle implements HandleRejectionDialog.IMhandleRejectionListener {

    @BindView(R.id.goodsdeimg)
    ImageView mGoodsImg;
    @BindView(R.id.goodsname)
    TextView mGoodsName;
    @BindView(R.id.goodsdetailsname)
    TextView mGoodsdetailsname;
    @BindView(R.id.goodsprice)
    TextView mGoodsPrice;
    @BindView(R.id.orderstate)
    TextView mOrderState;
    @BindView(R.id.reason)
    TextView mReason;
    @BindView(R.id.supplement)
    TextView mSupplement;
    @BindView(R.id.ordermoney)
    TextView mOrderMoney;
    @BindView(R.id.one_img)
    ImageView mOneImg;
    @BindView(R.id.two_img)
    ImageView mTwoImg;
    @BindView(R.id.three_img)
    ImageView mThreeImg;
    private String refundid;


    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("售后处理");
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_after_processing, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        String orderid = getIntent().getStringExtra(OrderFragment.ORDER_ID);
        getAfterSale(orderid);
    }

    //获取接口数据
    private void getAfterSale(String orderid) {
        showLoading();
        NetworkManager.afterSale(orderid, PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                dissmissLoading();
                successAfterSale(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                dissmissLoading();
            }
        });
    }

    private void successAfterSale(String result) {
        AfterSaleBean bean = new Gson().fromJson(result, AfterSaleBean.class);
        Glide.with(this).load(bean.getData().getOrderfind().getGoodsdeimg()).into(mGoodsImg);//商品图片
        mGoodsName.setText(bean.getData().getOrderfind().getGoodsname());//商品名称
        mGoodsdetailsname.setText(bean.getData().getOrderfind().getGoodsdetailsname());//商品规格
        mGoodsPrice.setText("¥  " + bean.getData().getOrderfind().getGoodsprice());//商品单价
        mOrderState.setText(bean.getData().getRefundfind().getOrderstate());//货物状态
        mReason.setText(bean.getData().getRefundfind().getReason());//退款原因
        mSupplement.setText("退款说明：" + bean.getData().getRefundfind().getSupplement());//退款说明
        mOrderMoney.setText("¥" + bean.getData().getOrdermoney());//退款金额
        Glide.with(this).load(bean.getData().getRefundfind().getRefundimg().get(0)).into(mOneImg);
        Glide.with(this).load(bean.getData().getRefundfind().getRefundimg().get(1)).into(mTwoImg);
        Glide.with(this).load(bean.getData().getRefundfind().getRefundimg().get(2)).into(mThreeImg);
        refundid = bean.getData().getRefundfind().getRefundid();
    }

    //驳回处理
    @OnClick(R.id.activity_after_processing_handle_rejection)
    public void handleRejectionClick() {
        HandleRejectionDialog dialog = new HandleRejectionDialog(this);
        dialog.setIMHandleRejection(this);
        dialog.show();
    }

    //驳回处理的弹框点击事件
    @Override
    public void OnHandleRejection(String mEditText) {
        if (TextUtils.isEmpty(mEditText)) {
            ToastUtil.showToast(AfterProcessingActivity.this, "请填写驳回处理");
            return;
        }
        rejectAgree("n", mEditText);
    }

    //同意处理
    @OnClick(R.id.activity_after_processing_agreed_settlement)
    public void agreedSettlementClick() {
        rejectAgree("y", "");
    }

    //接口判断是否处理问题
    private void rejectAgree(final String confirm, String message) {
        NetworkManager.rejectAgree(PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), refundid, confirm, message, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                if (confirm.equals("y")) {
                    ToastUtil.showToast(AfterProcessingActivity.this, "同意处理成功");
                } else {
                    ToastUtil.showToast(AfterProcessingActivity.this, "驳回处理成功");
                }
                finish();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

}
